from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
import sys
import functions, service
class Ui_MainWindow(QMainWindow):
    # 构造方法
    def __init__(self):
        super(Ui_MainWindow, self).__init__()
        self.setWindowFlags(QtCore.Qt.MSWindowsFixedSizeDialogHint)  # 只显示最小化和关闭按钮
        self.setupUi(self)  # 初始化窗体设置
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(715, 718)
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        MainWindow.setFont(font)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
        self.tabWidget.setGeometry(QtCore.QRect(0, 0, 721, 721))
        self.tabWidget.setObjectName("tabWidget")
        self.tab_2 = QtWidgets.QWidget()
        self.tab_2.setObjectName("tab_2")
        self.groupBox_4 = QtWidgets.QGroupBox(self.tab_2)
        self.groupBox_4.setGeometry(QtCore.QRect(0, 490, 711, 151))
        self.groupBox_4.setObjectName("groupBox_4")
        self.lineEdit_8 = QtWidgets.QLineEdit(self.groupBox_4)
        self.lineEdit_8.setGeometry(QtCore.QRect(90, 30, 121, 31))
        self.lineEdit_8.setObjectName("lineEdit_8")
        self.label_9 = QtWidgets.QLabel(self.groupBox_4)
        self.label_9.setGeometry(QtCore.QRect(20, 30, 72, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        self.label_9.setFont(font)
        self.label_9.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.label_9.setObjectName("label_9")
        self.lineEdit_9 = QtWidgets.QLineEdit(self.groupBox_4)
        self.lineEdit_9.setGeometry(QtCore.QRect(320, 30, 121, 31))
        self.lineEdit_9.setObjectName("lineEdit_9")
        self.label_10 = QtWidgets.QLabel(self.groupBox_4)
        self.label_10.setGeometry(QtCore.QRect(250, 30, 72, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        self.label_10.setFont(font)
        self.label_10.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.label_10.setObjectName("label_10")
        self.label_14 = QtWidgets.QLabel(self.groupBox_4)
        self.label_14.setGeometry(QtCore.QRect(471, 30, 91, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        self.label_14.setFont(font)
        self.label_14.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.label_14.setObjectName("label_14")
        self.lineEdit_12 = QtWidgets.QLineEdit(self.groupBox_4)
        self.lineEdit_12.setGeometry(QtCore.QRect(560, 30, 121, 31))
        self.lineEdit_12.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEdit_12.setObjectName("lineEdit_12")
        self.pushButton_5 = QtWidgets.QPushButton(self.groupBox_4)
        self.pushButton_5.setGeometry(QtCore.QRect(600, 90, 81, 31))
        self.pushButton_5.setObjectName("pushButton_5")
        self.groupBox_5 = QtWidgets.QGroupBox(self.tab_2)
        self.groupBox_5.setGeometry(QtCore.QRect(250, 0, 481, 471))
        self.groupBox_5.setObjectName("groupBox_5")
        self.tableWidget_2 = QtWidgets.QTableWidget(self.groupBox_5)
        self.tableWidget_2.setGeometry(QtCore.QRect(10, 30, 441, 391))
        self.tableWidget_2.setObjectName("tableWidget_2")
        self.tableWidget_2.setColumnCount(0)
        self.tableWidget_2.setRowCount(0)
        self.pushButton_6 = QtWidgets.QPushButton(self.groupBox_5)
        self.pushButton_6.setGeometry(QtCore.QRect(110, 430, 81, 31))
        self.pushButton_6.setObjectName("pushButton_6")
        self.pushButton_7 = QtWidgets.QPushButton(self.groupBox_5)
        self.pushButton_7.setGeometry(QtCore.QRect(290, 430, 81, 31))
        self.pushButton_7.setObjectName("pushButton_7")
        self.groupBox_6 = QtWidgets.QGroupBox(self.tab_2)
        self.groupBox_6.setGeometry(QtCore.QRect(0, 0, 241, 471))
        self.groupBox_6.setObjectName("groupBox_6")
        self.label_15 = QtWidgets.QLabel(self.groupBox_6)
        self.label_15.setGeometry(QtCore.QRect(20, 60, 72, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        self.label_15.setFont(font)
        self.label_15.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.label_15.setObjectName("label_15")
        self.lineEdit_13 = QtWidgets.QLineEdit(self.groupBox_6)
        self.lineEdit_13.setGeometry(QtCore.QRect(100, 60, 121, 31))
        self.lineEdit_13.setObjectName("lineEdit_13")
        self.lineEdit_14 = QtWidgets.QLineEdit(self.groupBox_6)
        self.lineEdit_14.setGeometry(QtCore.QRect(100, 190, 121, 31))
        self.lineEdit_14.setObjectName("lineEdit_14")
        self.label_16 = QtWidgets.QLabel(self.groupBox_6)
        self.label_16.setGeometry(QtCore.QRect(20, 190, 72, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        self.label_16.setFont(font)
        self.label_16.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.label_16.setObjectName("label_16")
        self.pushButton_8 = QtWidgets.QPushButton(self.groupBox_6)
        self.pushButton_8.setGeometry(QtCore.QRect(80, 330, 81, 31))
        self.pushButton_8.setObjectName("pushButton_8")
        self.tabWidget.addTab(self.tab_2, "")
        self.tab_4 = QtWidgets.QWidget()
        self.tab_4.setObjectName("tab_4")
        self.groupBox_9 = QtWidgets.QGroupBox(self.tab_4)
        self.groupBox_9.setGeometry(QtCore.QRect(0, 0, 291, 671))
        self.groupBox_9.setObjectName("groupBox_9")
        self.label_19 = QtWidgets.QLabel(self.groupBox_9)
        self.label_19.setGeometry(QtCore.QRect(20, 110, 72, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        self.label_19.setFont(font)
        self.label_19.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.label_19.setObjectName("label_19")
        self.lineEdit_17 = QtWidgets.QLineEdit(self.groupBox_9)
        self.lineEdit_17.setGeometry(QtCore.QRect(140, 110, 121, 31))
        self.lineEdit_17.setObjectName("lineEdit_17")
        self.lineEdit_18 = QtWidgets.QLineEdit(self.groupBox_9)
        self.lineEdit_18.setGeometry(QtCore.QRect(140, 260, 121, 31))
        self.lineEdit_18.setObjectName("lineEdit_18")
        self.label_20 = QtWidgets.QLabel(self.groupBox_9)
        self.label_20.setGeometry(QtCore.QRect(20, 260, 72, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        self.label_20.setFont(font)
        self.label_20.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.label_20.setObjectName("label_20")
        self.pushButton_10 = QtWidgets.QPushButton(self.groupBox_9)
        self.pushButton_10.setGeometry(QtCore.QRect(110, 400, 81, 31))
        self.pushButton_10.setObjectName("pushButton_10")
        self.groupBox_10 = QtWidgets.QGroupBox(self.tab_4)
        self.groupBox_10.setGeometry(QtCore.QRect(290, 0, 411, 311))
        self.groupBox_10.setObjectName("groupBox_10")
        self.tableWidget_4 = QtWidgets.QTableWidget(self.groupBox_10)
        self.tableWidget_4.setGeometry(QtCore.QRect(20, 30, 341, 231))
        self.tableWidget_4.setObjectName("tableWidget_4")
        self.tableWidget_4.setColumnCount(0)
        self.tableWidget_4.setRowCount(0)
        self.pushButton_11 = QtWidgets.QPushButton(self.groupBox_10)
        self.pushButton_11.setGeometry(QtCore.QRect(300, 270, 81, 31))
        self.pushButton_11.setObjectName("pushButton_11")
        self.groupBox_11 = QtWidgets.QGroupBox(self.tab_4)
        self.groupBox_11.setGeometry(QtCore.QRect(290, 330, 391, 341))
        self.groupBox_11.setObjectName("groupBox_11")
        self.tableWidget_5 = QtWidgets.QTableWidget(self.groupBox_11)
        self.tableWidget_5.setGeometry(QtCore.QRect(20, 30, 341, 261))
        self.tableWidget_5.setObjectName("tableWidget_5")
        self.tableWidget_5.setColumnCount(0)
        self.tableWidget_5.setRowCount(0)
        self.pushButton_12 = QtWidgets.QPushButton(self.groupBox_11)
        self.pushButton_12.setGeometry(QtCore.QRect(300, 300, 81, 31))
        self.pushButton_12.setObjectName("pushButton_12")
        self.tabWidget.addTab(self.tab_4, "")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        self.tabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        # 初始化权限
        self.init()

        # 课程信息管理页面begin
        # 添加课程信息按钮
        self.pushButton_5.clicked.connect(self.insert_course)
        # 查询课程信息按钮
        self.pushButton_8.clicked.connect(self.query_course)
        # 删除课程信息按钮
        self.pushButton_6.clicked.connect(self.delete_course)
        # 保存课程信息按钮
        self.pushButton_7.clicked.connect(self.update_course)
        # 课程信息管理页面end
        # 选课页面begin
        # 查询课程信息
        self.pushButton_10.clicked.connect(self.query_course_student)
        # 提交选课
        self.pushButton_11.clicked.connect(self.commit_choose)
        # 查询选课
        self.query_choose()
        # 取消选课
        self.pushButton_12.clicked.connect(self.cancel_choose)
        # 选课页面end


    def cancel_choose(self):
        # 遍历获取checkbox的状态
        checked_files = []
        for i in range(len(self.choose)):
            state = self.tableWidget_5.item(i, 0).checkState()
            if state == QtCore.Qt.Checked:
                checked_files.append(self.choose[i])
        if len(checked_files) == 0:
            QMessageBox.information(None, '提示', "请先勾选要取消的选课！", QMessageBox.Ok)
            return
        for i in checked_files:
            functions.cancel_choose(i[0])
        QMessageBox.information(None, '提示', "取消成功！", QMessageBox.Ok)
        self.query_course_student()
        self.query_choose()

    def commit_choose(self):
        # 遍历获取checkbox的状态
        checked_files = []
        for i in range(len(self.course_student)):
            state = self.tableWidget_4.item(i, 0).checkState()
            if state == QtCore.Qt.Checked:
                checked_files.append(self.course_student[i])
        if len(checked_files) == 0:
            QMessageBox.information(None, '提示', "请先勾选要选择的课程！", QMessageBox.Ok)
            return
        grade, ok = QInputDialog.getText(self, "请输入选课学期", "请输入选课学期:")
        if ok:
            for i in checked_files:
                data = functions.check_choose(service.user_id, i[0], grade)
                if len(data) > 0:
                    QMessageBox.information(None, '提示', "请勿重复选课！", QMessageBox.Ok)
                    return
            for i in checked_files:
                functions.choose(service.user_id, i[0], grade)
        QMessageBox.information(None, '提示', "选课成功！", QMessageBox.Ok)
        self.query_course_student()
        self.query_choose()

    def query_choose(self):
        self.choose = functions.query_choose(service.user_id)
        self.settablewidget_5()

    def settablewidget_5(self):
        row = len(self.choose)
        if row == 0:
            # 没数据，不显示东西
            self.tableWidget_5.setColumnCount(0)
            self.tableWidget_5.setRowCount(0)
            return
        col = len(self.choose[0])
        self.tableWidget_5.setRowCount(len(self.choose))
        self.tableWidget_5.setColumnCount(len(self.choose[0]) + 1)
        self.tableWidget_5.setHorizontalHeaderLabels(['', '选课ID', '学生名', '课程名', '授课教师', '上课地点', '学期'])
        for i in range(len(self.choose)):
            # 每行添加一个checkbox
            checkbox = QtWidgets.QTableWidgetItem()
            checkbox.setCheckState(QtCore.Qt.Unchecked)
            self.tableWidget_5.setItem(i, 0, checkbox)
            for j in range(len(self.choose[0])):
                cell = self.choose[i][j]
                item = QTableWidgetItem(str(cell))
                item.setFlags(Qt.ItemIsEnabled)
                self.tableWidget_5.setItem(i, j + 1, item)
        self.tableWidget_5.resizeColumnToContents(0)  # 列宽随内容改变
        self.tableWidget_5.setAlternatingRowColors(True)  # 表格颜色交错显示
        self.tableWidget_5.verticalHeader().setVisible(False)  # 序号不显示
        self.tableWidget_5.horizontalHeader().setStretchLastSection(True)  # 最后一列自动填充容器
        self.tableWidget_5.setWordWrap(True)

    def query_course_student(self):
        name = self.lineEdit_17.text()
        teacher = self.lineEdit_18.text()
        self.course_student = functions.query_course(name, teacher)
        self.settablewidget_4()

    def settablewidget_4(self):
        row = len(self.course_student)
        if row == 0:
            # 没数据，不显示东西
            self.tableWidget_4.setColumnCount(0)
            self.tableWidget_4.setRowCount(0)
            QMessageBox.information(None, '提示', '系统中不存在课程信息数据！', QMessageBox.Ok)
            return
        col = len(self.course_student[0])
        self.tableWidget_4.setRowCount(len(self.course_student))
        self.tableWidget_4.setColumnCount(len(self.course_student[0]) + 1)
        self.tableWidget_4.setHorizontalHeaderLabels(['', '课程ID', '课程名', '授课教师', '上课地点'])
        for i in range(len(self.course_student)):
            # 每行添加一个checkbox
            checkbox = QtWidgets.QTableWidgetItem()
            checkbox.setCheckState(QtCore.Qt.Unchecked)
            self.tableWidget_4.setItem(i, 0, checkbox)
            for j in range(len(self.course_student[0])):
                cell = self.course_student[i][j]
                item = QTableWidgetItem(str(cell))
                item.setFlags(Qt.ItemIsEnabled)
                self.tableWidget_4.setItem(i, j + 1, item)
        self.tableWidget_4.resizeColumnToContents(0)  # 列宽随内容改变
        self.tableWidget_4.setAlternatingRowColors(True)  # 表格颜色交错显示
        self.tableWidget_4.verticalHeader().setVisible(False)  # 序号不显示
        self.tableWidget_4.horizontalHeader().setStretchLastSection(True)  # 最后一列自动填充容器
        self.tableWidget_4.setWordWrap(True)

    def update_course(self):
        # 遍历获取checkbox的状态
        checked_files = []
        for i in range(len(self.course)):
            state = self.tableWidget_2.item(i, 0).checkState()
            if state == QtCore.Qt.Checked:
                route = (self.tableWidget_2.item(i, 1).text(),
                         self.tableWidget_2.item(i, 2).text(),
                         self.tableWidget_2.item(i, 3).text(),
                         self.tableWidget_2.item(i, 4).text()
                         )
                checked_files.append(route)
        if len(checked_files) == 0:
            QMessageBox.information(None, '提示', "请先勾选要修改的数据！", QMessageBox.Ok)
            return
        for i in checked_files:
            functions.update_course(i[0], i[1], i[2], i[3])
        QMessageBox.information(None, '提示', "修改成功！", QMessageBox.Ok)
        self.query_course()

    def delete_course(self):
        # 遍历获取checkbox的状态
        checked_files = []
        for i in range(len(self.course)):
            state = self.tableWidget_2.item(i, 0).checkState()
            if state == QtCore.Qt.Checked:
                checked_files.append(self.course[i])
        if len(checked_files) == 0:
            QMessageBox.information(None, '提示', "请先勾选要删除的数据！", QMessageBox.Ok)
            return
        for i in checked_files:
            functions.delete_course(i[0])
        QMessageBox.information(None, '提示', "删除成功！", QMessageBox.Ok)
        self.query_course()

    def query_course(self):
        name = self.lineEdit_13.text()
        teacher = self.lineEdit_14.text()
        self.course = functions.query_course(name, teacher)
        self.settablewidget_2()

    def settablewidget_2(self):
        row = len(self.course)
        if row == 0:
            # 没数据，不显示东西
            self.tableWidget_2.setColumnCount(0)
            self.tableWidget_2.setRowCount(0)
            QMessageBox.information(None, '提示', '系统中不存在课程信息数据！', QMessageBox.Ok)
            return
        col = len(self.course[0])
        self.tableWidget_2.setRowCount(len(self.course))
        self.tableWidget_2.setColumnCount(len(self.course[0]) + 1)
        self.tableWidget_2.setHorizontalHeaderLabels(['', '课程ID', '课程名', '授课教师', '上课地点'])
        for i in range(len(self.course)):
            # 每行添加一个checkbox
            checkbox = QtWidgets.QTableWidgetItem()
            checkbox.setCheckState(QtCore.Qt.Unchecked)
            self.tableWidget_2.setItem(i, 0, checkbox)
            for j in range(len(self.course[0])):
                cell = self.course[i][j]
                item = QTableWidgetItem(str(cell))
                if j == 0:
                    item.setFlags(Qt.ItemIsEnabled)
                self.tableWidget_2.setItem(i, j + 1, item)
        self.tableWidget_2.resizeColumnToContents(0)  # 列宽随内容改变
        self.tableWidget_2.setAlternatingRowColors(True)  # 表格颜色交错显示
        self.tableWidget_2.verticalHeader().setVisible(False)  # 序号不显示
        self.tableWidget_2.horizontalHeader().setStretchLastSection(True)  # 最后一列自动填充容器
        self.tableWidget_2.setWordWrap(True)

    def insert_course(self):
        name = self.lineEdit_8.text()
        teacher = self.lineEdit_9.text()
        address = self.lineEdit_12.text()
        if name == '' or teacher == '' or address == '':
            QMessageBox.information(None, '提示', '课程信息不能为空！', QtWidgets.QMessageBox.Ok)
            return
        data = functions.query_course_by_name(name)
        if len(data) != 0:
            QMessageBox.information(None, '提示', '该课程已经录入，请勿重复录入！', QtWidgets.QMessageBox.Ok)
            return
        functions.insert_course(name, teacher, address)
        QMessageBox.information(None, '提示', '添加成功！', QtWidgets.QMessageBox.Ok)
        self.lineEdit_8.setText('')
        self.lineEdit_9.setText('')
        self.lineEdit_12.setText('')
        self.query_course()


    def init(self):
        username = service.username
        if username == 'admin':
            self.tabWidget.removeTab(1)
        else:
            self.tabWidget.removeTab(0)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "学生选课系统"))
        self.groupBox_4.setTitle(_translate("MainWindow", "添加数据"))
        self.label_9.setText(_translate("MainWindow", "课程名："))
        self.label_10.setText(_translate("MainWindow", "教师名："))
        self.label_14.setText(_translate("MainWindow", "上课地点："))
        self.pushButton_5.setText(_translate("MainWindow", "添加"))
        self.groupBox_5.setTitle(_translate("MainWindow", "查询结果"))
        self.pushButton_6.setText(_translate("MainWindow", "删除"))
        self.pushButton_7.setText(_translate("MainWindow", "保存"))
        self.groupBox_6.setTitle(_translate("MainWindow", "查询设置"))
        self.label_15.setText(_translate("MainWindow", "课程名："))
        self.label_16.setText(_translate("MainWindow", "教师名："))
        self.pushButton_8.setText(_translate("MainWindow", "查询"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "课程信息管理"))
        self.groupBox_9.setTitle(_translate("MainWindow", "查询设置"))
        self.label_19.setText(_translate("MainWindow", "课程名："))
        self.label_20.setText(_translate("MainWindow", "教师名："))
        self.pushButton_10.setText(_translate("MainWindow", "查询"))
        self.groupBox_10.setTitle(_translate("MainWindow", "查询结果"))
        self.pushButton_11.setText(_translate("MainWindow", "提交选课"))
        self.groupBox_11.setTitle(_translate("MainWindow", "已选课程"))
        self.pushButton_12.setText(_translate("MainWindow", "取消选课"))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_4), _translate("MainWindow", "选课"))
